package com.rongzhe.house.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jaygoo.widget.RangeSeekBar;
import com.rongzhe.house.R;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.presenter.BasePresenter;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by huang on 2017/11/27.
 */

public class OtherFilterFragment extends FilterFragment {
    @BindView(R.id.seek_bar_rent)
    RangeSeekBar seekBarRent;
    @BindView(R.id.btn_reset)
    Button btnReset;
    @BindView(R.id.btn_ok)
    Button btnOk;
    Unbinder unbinder;
    @BindView(R.id.text_rent_range)
    TextView textRentRange;
    private int mMinRent;
    private int mMaxRent;


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more_filter, container, false);
        unbinder = ButterKnife.bind(this, view);
        seekBarRent.setValue(0, 100);
        seekBarRent.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
                mMinRent = (int) (min * 100);
                mMaxRent = (int) (max * 100);
                String maxShowing;
                if(max == seekBarRent.getMax()) {
                    mMaxRent = -1;
                    maxShowing = getString(R.string.no_limit);
                } else {
                    maxShowing = mMaxRent + "";
                }
                String showing;
                if(min == 0 && max == 100) {
                    showing = getString(R.string.no_limit);
                    btnReset.setEnabled(false);
                } else {
                    showing = String.format(getString(R.string.format_rent_showing), mMinRent+"",
                            maxShowing);
                    btnReset.setEnabled(true);
                }
                textRentRange.setText(showing);

            }
        });
        return view;
    }

    @OnClick(R.id.btn_ok)
    public void onOkClick() {
        if (mMinRent != 0 || (mMaxRent != -1 && mMaxRent != 0)) {
            HouseManager.getInstance().setRentStart(new BigDecimal(mMinRent));
            HouseManager.getInstance().setRentEnd(mMaxRent == -1 ? null : new BigDecimal(mMaxRent));
        } else {
            HouseManager.getInstance().setRentStart(null);
            HouseManager.getInstance().setRentEnd(null);
        }
        finishFragment();
    }

    @OnClick(R.id.btn_reset)
    public void onResetClick() {
        seekBarRent.setValue(0,  100);
        HouseManager.getInstance().setRentStart(null);
        HouseManager.getInstance().setRentEnd(null);
        finishFragment();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
